package main

import (
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"log"
	"net/http"
	"strings"
)

// Gzip 中间件使用 gzip 方案来对 HTTP 响应进行压缩。
func main() {

	e := echo.New()
	//用法
	e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
		Skipper: func(c echo.Context) bool {
			uri := c.Request().RequestURI
			if strings.Contains(uri, "/index") {
				log.Println("uri:", uri, "--->>>Skipper-true")
				return true
			}
			log.Println("uri:", uri, "--->>>Skipper-false")
			return false
		},
		Level: 5,
	}))

	e.GET("/index", Index)
	e.GET("/body", Body)

	e.Start(":80")
}

func Index(c echo.Context) error {
	return c.String(http.StatusOK, "index success!")
}

func Body(c echo.Context) error {
	return c.JSON(http.StatusOK, studentList())
}

type Student struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func studentList() []Student {
	var studentList []Student = make([]Student, 0)
	for i := 0; i < 100; i++ {
		studentList = append(studentList, Student{
			Name: "name" + fmt.Sprintf("%d", i),
			Age:  i,
		})
	}
	return studentList
}
